home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscClipTextPalette / MiscClipText.subproj / MiscClipTextField.m < prev    next >
Encoding:
Text File  |  1995-07-13  |  5.8 KB  |  245 lines

  1. //
  2. //    MiscClipTextField.m -- a TextField subclass for displaying long string 
  3. //                           values
  4. //        Written and Copyright (c) 1995 by Balazs Pataki. 
  5. //                Version 1.0.  All rights reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the author
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //    
  14.  
  15. #import <appkit/appkit.h>
  16. #import    <misckit/MiscString.h>
  17. #import <objc/objc-runtime.h>
  18.  
  19. #import "MiscClipTextField.h"
  20. #import "MiscClipTextFieldCell.h"
  21.  
  22.  
  23. #define    CLASS_NAME        "MiscClipTextField"
  24. #define    CLASS_VERSION    1
  25. #define CELL_CLASS        [MiscClipTextFieldCell class]
  26.  
  27.  
  28. #define DELIMITERS        [delimiters stringValue]
  29.  
  30.  
  31. static id    CellClass;    // Class variable of MiscClipTextField
  32.  
  33.  
  34.  
  35. /*
  36.                         ********************************
  37.                         *                              *
  38.                         *      MiscClipTextField       *
  39.                         *                              *
  40.                         ********************************
  41. */
  42.  
  43. @implementation MiscClipTextField
  44.  
  45. + initialize
  46. // Set class version
  47. {
  48.     if (self == objc_lookUpClass(CLASS_NAME))  {
  49.         [self setVersion:CLASS_VERSION];
  50.         CellClass = CELL_CLASS;
  51.     }
  52.     return self;
  53. }
  54.  
  55. + setCellClass:classId
  56. {
  57.     CellClass=classId;
  58.     return self;
  59. }
  60.  
  61.  
  62. - initFrame:(const NXRect *)rect
  63. // Designated initializer
  64. {
  65.     id    oldCell;
  66.     
  67.     [super initFrame:rect];
  68.                                         /* Plug in our own cell                */
  69.     oldCell = [self setCell:[[CellClass alloc] initTextCell:""]];
  70.     [oldCell free];
  71.     
  72.     return self;
  73. }
  74.  
  75. - setStringValue:(const char*)stringValue
  76. {
  77.     id theCell = [self cell];
  78.     // Change the alignemt to origAlignment because if previously there was
  79.     // a triple-click the cells alignemnt changed to left, which may not have
  80.     // been our original alignment
  81.     [theCell setSelectable:NO];        /* Disable selecting and scrolling        */
  82.     [theCell setScrollable:NO];
  83.     [theCell setAlignment:origAlignment];
  84.     return [super setStringValue:stringValue];
  85. }
  86.  
  87. - setAlignment:(int)align
  88. {
  89.     // Save alignment to change it back after a triple-click (triple-click 
  90.     // event changes the cell to display the cell left aligned and we have to
  91.     // take care to undo it) 
  92.     origAlignment = align;
  93.     return self;
  94. }
  95.  
  96.  
  97. - resetStringValue:sender
  98. {
  99.     id theCell = [self cell];
  100.     
  101.     [theCell setSelectable:NO];        /* Disable selecting and scrolling        */
  102.     [theCell setScrollable:NO];
  103.     [theCell setAlignment:origAlignment];
  104.     [[self cell] resetStringValue:sender];
  105.     return self;
  106. }
  107.  
  108.  
  109. - setClipEnabled:(BOOL)flag
  110. // Sets whether the next `setStringValue:' message should clip the text or not 
  111. {
  112.     [[self cell] setClipEnabled:flag];
  113.     return self;
  114. }
  115.  
  116.  
  117. - setClipOnRight:(BOOL)flag
  118. // If flag is YES clipping happens on the right, otherwise on the left of the 
  119. // string in the cell. 
  120. {
  121.     [[self cell] setClipOnRight:flag];
  122.     return self;
  123. }
  124.  
  125.  
  126. - setClipperString:(const char*)aString
  127. // Sets  `aString' as the string that is displayed in place of the clipped part
  128. // of the original string. 
  129. {
  130.     [[self cell] setClipperString:aString];
  131.     return self;
  132. }
  133.  
  134.  
  135. - setClipDelimiters:(const char*)delimChars
  136. // Sets  `delimChars' as the delimiters by which the clipping has to happen. 
  137. // If `delimChars' is NULL or an empty string clipping will occur to any 
  138. // character in the string.
  139. {
  140.     [[self cell] setClipDelimiters:delimChars];
  141.     return self;
  142. }
  143.  
  144. - sizeTo:(NXCoord)width :(NXCoord)height
  145. // Adjusts the current text to the new size and redisplays it
  146. {
  147.     [super sizeTo:width :height];
  148.     [self resetStringValue:self];
  149.     return self;
  150. }
  151.  
  152.  
  153. - mouseDown:(NXEvent *)event
  154. // In case of triple-click event displays the full text instead of the clipped
  155. // one and allows selecting and scrolling the string
  156. // NOTE: This trick alters the alignment of the cell and changes it to 
  157. //         align its text to left and to truncate the text - don't know any
  158. //         workaround (maybe we should stay with left aligned text only :-(
  159.     // Handling only triple-click event
  160.     if (event->data.mouse.click == 3) {
  161.         id    theCell  = [self cell];        
  162.         [[self window] disableDisplay];
  163.         [theCell setSelectable:YES];
  164.         [theCell setScrollable:YES];
  165.         [theCell setClipEnabled:NO];     /* Turn off clipping for a moment    */
  166.         [theCell resetStringValue:self];
  167.         [theCell setClipEnabled:YES];    /* ... and turn it back                */
  168.         [[self window] reenableDisplay];
  169.         [self display];
  170.     }
  171.     return [super mouseDown:event];
  172. }
  173.  
  174. - (const char*)fullStringValue;    { return [[self cell] fullStringValue]; }
  175. - clipper                    { return [[self cell] clipper];            }
  176. - delimiters                { return [[self cell] delimiters];        }
  177. - (BOOL) doesClipOnRight    { return [[self cell] doesClipOnRight];    }
  178. - (BOOL) isClipEnabled        { return [[self cell] isClipEnabled];    }
  179.  
  180. - write:(NXTypedStream *)stream
  181. {
  182.     [super write:stream];
  183.     
  184.     NXWriteType(stream, @encode(int), &origAlignment);
  185.    
  186.     return self;
  187. }
  188.  
  189.  
  190. - read:(NXTypedStream *)stream
  191. {
  192.     [super read:stream];
  193.    
  194.     NXReadType(stream, @encode(int), &origAlignment);
  195.  
  196.     return self;
  197. }
  198.  
  199. @end
  200.  
  201.  
  202. /*
  203.                             ********************************
  204.                             *                              *
  205.                             *  MiscClipTextField(IBStuff)  *
  206.                             *                              *
  207.                             * -- InterfaceBuilder Stuff--  *
  208.                             *                              *
  209.                             ********************************
  210. */
  211.  
  212. @implementation MiscClipTextField(IBStuff)
  213.  
  214. - (const char *)getInspectorClassName
  215. // Return the class name of our inspector.
  216. {
  217.     return "MiscClipTextFieldInspector";
  218. }
  219.  
  220. @end
  221.  
  222.  
  223. /*
  224.                             ********************************
  225.                             *                              *
  226.                             *   MiscClipTextField(Test)    *
  227.                             *                              *
  228.                             * -- Debug and Test Methods -- *
  229.                             *                              *
  230.                             ********************************
  231. */
  232.  
  233. @implementation MiscClipTextField(Test)
  234.  
  235. - changeFont:sender
  236. {
  237.     [[self cell] setFont:[sender selFont]];
  238.     [[self cell] resetStringValue:self];
  239.     return self;
  240. }
  241.  
  242. @end
  243.  
  244.